package cn.guxinhu.billsystem.utils;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Guxinhu
 * @create 2021-06-23 15:27
 */
public class Timer implements Runnable {
    //定时器
    Boolean ifLive = true;

    @Override
    public void run() {
        while (ifLive) {
            System.out.println(222);
            ifLive = false;
        }
    }

    public static void setTimer(String time, boolean live) throws Exception {
        Runnable runnable = new Runnable() {
            Boolean ifLive = live;

            public void run() {
                while (ifLive) {
                    System.out.println(222);
                    ifLive = false;
                }
            }
        };
        long longtime = (TimeTools.setCalendar(time).getTimeInMillis() - TimeTools.setCalendar().getTimeInMillis()) / 1000;
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.schedule(runnable, longtime, TimeUnit.SECONDS);
    }


}
